package com.zmn.plat.admin.controller.service.item.config;

import java.util.List;

import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.util.CollectionUtils;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import com.zmn.common.dto2.ResponseDTO;
import com.zmn.common.utils.number.NumberUtil;
import com.zmn.plat.common.exception.PlatException;
import com.zmn.plat.model.vo.service.item.config.ServiceItemTimeConfigVO;

import lombok.extern.slf4j.Slf4j;

/**
 * @author tanbiao
 * @description
 * @modifier
 * @since 2020/7/30 15:19
 **/
@RequestMapping("/service/item/timeConfig/")
@RestController
@Slf4j
public class ServiceItemTimeConfigModifyController extends ServiceItemTimeConfigBaseController {

    /**
     * 添加
     *
     * @param configList
     * @author tanbiao
     * @since 2019-11-21 18:57:24
     */
    @RequestMapping("addEdit")
    @RequiresPermissions("/service/item/timeConfig/detail.action")
    public ResponseDTO<Integer> addEdit(@RequestBody List<ServiceItemTimeConfigVO> configList,
        @RequestParam(value = "itemId") Integer itemId) {
        if (CollectionUtils.isEmpty(configList) || !NumberUtil.isPositiveInteger(itemId)) {
            return ResponseDTO.fail("参数不能为空,请检查");
        }

        try {
            Integer result = faultItemTimeConfigBService.addEdit(configList, itemId, getMcStaffRealName());
            return ResponseDTO.success(result, "操作成功！");
        } catch (PlatException e) {
            return ResponseDTO.fail(e.getMessage());
        } catch (Exception e) {
            log.error("deleteGroup error..", e);
            return ResponseDTO.fail("添加失败");
        }
    }

    /**
     * 删除时间段
     *
     * @param configId
     * @author tanbiao
     * @since 2019-11-21 18:57:24
     */
    @RequestMapping("deleteById")
    // @RequiresPermissions("/service/item/timeConfig/detail.action")
    public ResponseDTO<Integer> deleteById(@RequestParam(value = "configId", defaultValue = "0") Integer configId) {
        if (!NumberUtil.isPositiveInteger(configId)) {
            return ResponseDTO.fail("参数不能为空,请检查");
        }
        Integer result = faultItemTimeConfigService.deleteByKey(configId);
        return ResponseDTO.success(result);
    }

    /**
     * 删除分组:一个分组的日期是一样的，通过传递第一个configID，查询出相应的数据，再删除
     *
     * @param configId
     * @author tanbiao
     * @since 2019-11-21 18:57:24
     */
    @RequestMapping("deleteGroup")
    @RequiresPermissions("/service/item/timeConfig/detail.action")
    public ResponseDTO<Integer> deleteGroup(@RequestParam(value = "configId", defaultValue = "0") Integer configId) {
        if (!NumberUtil.isPositiveInteger(configId)) {
            return ResponseDTO.fail("参数不能为空,请检查");
        }

        try {
            Integer result = faultItemTimeConfigBService.deleteGroupByConfigId(configId);
            return ResponseDTO.success(result);
        } catch (PlatException e) {
            return ResponseDTO.fail(e.getMessage());
        } catch (Exception e) {
            log.error("deleteGroup error..", e);
            return ResponseDTO.fail("删除失败");
        }

    }
}
